package parser

import (
	"learn-go/15.crawler/engine"
	"regexp"
)

// <a href="http://album.zhenai.com/u/1491978891" target="_blank">王天笑</a>
const cityRe = `<a href="(http://album.zhenai.com/u/[0-9]+)"[^>]*>([^<]+)</a>`

func ParseCity(contents []byte) engine.ParseResult {
	re := regexp.MustCompile(cityRe)
	matches := re.FindAllSubmatch(contents, -1)

	result := engine.ParseResult{}
	for _, m := range matches {
		name := string(m[2]) // 因为使用了闭包，循环变量提前存下
		result.Items = append(result.Items, "User "+name)
		result.Requests = append(result.Requests, engine.Request{
			Url: string(m[1]),
			ParserFunc: func(c []byte) engine.ParseResult {
				return ParseProfile(c, name) // 为了传姓名，有的网页名称在上级网页
			},
		})
	}

	return result
}
